package com.ansion.commonchat.contact.systemcontact

import android.content.Context
import android.content.Intent
import android.provider.BaseColumns
import android.provider.ContactsContract
import android.text.TextUtils
import androidx.activity.result.contract.ActivityResultContract
import androidx.collection.LongSparseArray
import com.ansion.basedb.dao.LoginedUserMgr
import com.ansion.basedb.dao.bean.SyncAdapterColumns
import com.ansion.commonchat.BaseApplication
import com.ansion.commonchat.util.ContactGuideHelper
import com.ansion.constant.BConstant

class MyCustomContract(name:String?,phone:String?) : ActivityResultContract<Int, String>() {
    private val name: String? = name
    private val phone: String? = phone
    override fun createIntent(context: Context, input: Int): Intent {
        val intent = Intent(Intent.ACTION_INSERT)
        //		intent.setType("vnd.android.cursor.dir/person");
        //		intent.setType("vnd.android.cursor.dir/contact");
//        intent.setType("vnd.android.cursor.dir/raw_contact")

        intent.setType(ContactsContract.RawContacts.CONTENT_TYPE)

        // 设置联系人属于你应用的帐户
        val userId = LoginedUserMgr.getCurrentLoginedUser().userId.toString()
        if (userId != null) {
            intent.putExtra(ContactsContract.RawContacts.ACCOUNT_TYPE, SyncAdapterColumns.ACCOUNT_TYPE)
            intent.putExtra(ContactsContract.RawContacts.ACCOUNT_NAME, userId)
        }

        // 联系人姓名
        if (!TextUtils.isEmpty(name)) {
            intent.putExtra(ContactsContract.Intents.Insert.NAME, name)
        }
        if (!TextUtils.isEmpty(phone)) {
            intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone)
        }
        return intent
    }

    override fun parseResult(resultCode: Int, intent: Intent?): String {
        //遍历，然后把新用户插入数据库
        ContactGuideHelper.onActivityResult(BConstant.Contact.REQUEST_CODE_EDIT_CONTACT)
        return intent?.getStringExtra("resultKey") ?: "No result"
    }
}